<template>
  <!-- 机构列表页 -->
  <div>
    <el-table v-if="tableData3.length>0" :data="tableData3" border style="width: 100%" >
      <el-table-column prop="ORGANIZATION_ID" label="渠道编号" width="180">
      </el-table-column>
      <el-table-column prop="ORGANIZATION_NAME" label="机构名称" width="300">
      </el-table-column>
      <el-table-column prop="ORGANIZATION_CONTACTS" label="联系人" width="180">
      </el-table-column>
      <el-table-column prop="ORGANIZATION_TEL" label="联系电话" width="160">
      </el-table-column>
      <el-table-column prop="ORGANIZATION_EMAIL" label="邮箱" width="200">
      </el-table-column>
       <el-table-column prop="ORGANIZATION_TIME" label="添加时间" width="200" :formatter="localDataFomatter">
      </el-table-column>      
      <el-table-column prop="ADMIN_NAME" label="渠道负责人" width="120">
       </el-table-column>
      <el-table-column prop="ORGANIZATION_LEVEL" label="星级" width="80">
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template scope="scope">
          <el-button type="text" size="media" @click="goAddCommunication(scope.row.ORGANIZATION_ID)">
            <i class="el-icon-view"> 沟通</i>
          </el-button>
          <el-button type="text" size="media" @click="goOrganizaitionEditer(scope.row.ORGANIZATION_ID)">
            <i class="el-icon-view"> 查看</i>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template scope="scope">
          <el-button type="text" size="media" @click="goAddCustom(scope.row.ORGANIZATION_ID)">
            <i class="el-icon-view"> 新增客户</i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination @current-change="handleCurrentChange" :current-page="currentPage" :page-size="10" layout="total, prev, pager, next" :total="pagecount">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import api from "@/js/api";
import getid from "@/js/getid";
export default {
  data() {
    return {
      pagecount: 0,
      currentPage: 1,
      tableData3: [],
      Sdate: {
        name: "",
        address: "",
        people: "",
        time: [],
        userId:''
      }
    };
  },
  props: {
    type: {
      type: String,
      default: ""
    }
  },
  methods: {
    localDataFomatter(row, column, cellValue) {
        if(cellValue){
      cellValue = cellValue + "Z";
      var data = new Date(cellValue);
      var dataString =
        data.getFullYear() +
        "/" +
        (data.getMonth() + 1 >= 10
          ? data.getMonth() + 1
          : "0" + (data.getMonth() + 1)) +
        "/" +
        (data.getDate() >= 10 ? data.getDate() : "0" + data.getDate()) +
        " " +
        (data.getHours() >= 10 ? data.getHours() : "0" + data.getHours()) +
        ":" +
        (data.getMinutes() >= 10 ? data.getMinutes() : "0" + data.getMinutes());
      return dataString;
        }
    },
    //
    handleCurrentChange(val) {
      this.currentPage = val;
      // console.log(val);
      this.Mselect(this.Sdate, true);
    },
    goAddCommunication(cid) {
      let data = {
        id: cid,
        type: "渠道"
      };
      this.$emit("goAddCommunication", data);
    },
    goAddCustom(cid) {
      this.$emit("goAddCustom", cid);
    },
    goOrganizaitionEditer(id) {
      this.$emit("goOrganizaitionEditer", id);
    },
    //多条件查询
    Mselect(Mdate, currentChange) {
      this.Sdate = Mdate;
   
      let that = this;
      var data = {
        name: Mdate.name == "" ? null : Mdate.name,
        address: Mdate.address == "" ? null : Mdate.address,
        people: Mdate.people == "" ? null : Mdate.people,
        userId: Mdate.userId == "" ? null : Mdate.userId,
      };
      if (Mdate.time.length > 0) {
        data.startTime = getid.getDateString(Mdate.time[0]);
        data.endTime = getid.getDateString(Mdate.time[1]);
      } else {
        data.startTime = null;
        data.endTime = null;
      }
      if (!currentChange) {
        this.currentPage = 1;
      }
      data.pageIndex = this.currentPage;
      data.pageSize = 10;
      var role = sessionStorage.getItem("userRole");
      if (role == "销售助理" || role == "销售主管") {
        // data.userId = null;
      } else {
        data.userId = sessionStorage.getItem("userId");
      }
        //  console.log(data);
      api.OrgnizationGetMutial(that, data, function(msg) {
        that.tableData3 = msg.Result;
        that.pagecount = msg.ListCount;
      });
    },
    //获取数据
    Getdata() {
      var role = sessionStorage.getItem("userRole");
      // console.log(role);
      if (role == "销售助理" || role == "销售主管") {
        var data = {
          pageIndex: this.currentPage,
          pageSize: 10,
          userId: sessionStorage.getItem("userId")
        };
        let that = this;       
        if (this.type == "待沟通") {
          let that = this;
          api.OrgnizationGetRemainList(that, data, function(msg) {
            that.tableData3 = msg.Result;
            that.pagecount = msg.ListCount;
          });
        } else {
          api.OrgnizationGetList(that, data, function(msg) {
            that.tableData3 = msg.Result;
            that.pagecount = msg.ListCount;
          });
        }
      } else {
        var data = {
          pageIndex: this.currentPage,
          pageSize: 10,
          userId: sessionStorage.getItem("userId")
        };
        let that = this;
        if (this.type == "待沟通") {
          let that = this;
          api.OrgnizationGetRemainListByUser(that, data, function(msg) {
            that.tableData3 = msg.Result;
            that.pagecount = msg.ListCount;
          });
        } else {
          api.OrgnizationGetUserList(that, data, function(msg) {
            that.tableData3 = msg.Result;
            that.pagecount = msg.ListCount;
          });
        }
      }
    }
  },
  created() {
    this.currentPage = 1;
    this.Getdata();
  }
};
</script>
